/**
 * 对layui常用方法的封装
 * @author 董其宾
 * @time 2018-8-24 15:27:08
 */
function SelfLayui(layui,$,table_id) {
    this.layui = layui;
    this.$ = $;
    this.table_id = table_id || 'list';

    // 获取search的变量,用于提交
    this.getSearch = function(id) {
        const key = {};

        id = id || '#_search'
        const arr = $(id).serializeArray();
        for(let i in arr) {
            const line = arr[i]
            key[line['name']] = line['value']
        }
        return key
    }

    //执行重载
    this.reload = function(key,table_id, sort) {
        table_id = table_id || this.table_id
        const where = {}
        sort = sort || ''

        if(JSON.stringify(key) != "{}") {
            //如果where为空
            where.key = key
        }

        where['sort'] = sort

        console.log(where)
        this.layui.table.reload(table_id, {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: where
        });
    }

    //批量删除
    this.delBatch = function(url,data_key,table_id) {
        var table_id = table_id || this.table_id;
        var data_key = data_key || 'ids';
        //通过复选框删除
        var layui = this.layui;
        var $ = this.$;
        layer.confirm("确认要删除吗，删除后不能恢复", {title: "删除确认"}, function (index) {
            layer.close(index);
            var checkStatus = layui.table.checkStatus(table_id)
                , data = checkStatus.data
                , ids = [];
            $.each(data, function (key, val) {
                ids.push(val.id);
            });
            var data_post = {};
            data_post[data_key] = ids;
            $.post(url, data_post, function (d) {
                layer.msg(d.msg);
                if (d.code == 0) {
                    //成功刷新页面
                    layui.table.reload(table_id);
                }
            });
        });
    }

    //单条删除
    this.del = function(url,table_id) {
        const msg = '确定删除此数据吗?'
        const reload = true
        this.getUrl(url, msg, reload, table_id)
    }

    //请求服务器
    this.getUrl = function(url, msg, reload, table_id, callback) {
        msg = msg || '确定执行此操作吗?'
        table_id = table_id || this.table_id;
        const $ = this.$;
        const layui = this.layui;
        layer.confirm(msg, function(index){
            //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
            layer.close(index);
            //向服务端发送删除指令
            $.getJSON(url , function(d) {
                if(callback) {
                    callback(d.data)
                } else {
                    layer.msg(d.msg);
                }
                if(d.code == 0) {
                    if(reload) {
                        layui.table.reload(table_id);
                    }
                }
            });
        });
    }

    //请求服务器
    this.postUrl = function(url, data, msg, reload, table_id, callback) {
        msg = msg || '确定执行此操作吗?'
        table_id = table_id || this.table_id;
        const $ = this.$;
        const layui = this.layui;
        layer.confirm(msg, function(index){
            //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
            layer.close(index);
            //向服务端发送删除指令
            $.post(url, data, function(d) {
                if(callback) {
                    callback(d.data)
                } else {
                    layer.msg(d.msg);
                }
                if(d.code == 0) {
                    if(reload) {
                        layui.table.reload(table_id);
                    }
                }
            });
        });
    }

    //打开添加/编辑页面
    this.openAdd = function(title , content,area) {
        area = area || ['1000px','800px'];
        layui.use('layer', function () {
            //请求修改页面
            layer.open({
                type: 2,
                title: [title, 'text-align:center'],
                area: area,
                content: content,
            });

        });
    }

    //切换按钮改变状态
    this.changeStatus = function(url,data,table_id, msg) {
        var $ = this.$;
        var layui = this.layui;

        table_id = table_id || this.table_id;

        if(!msg && typeof data=='string' && data.indexOf('确定') !== -1) {
            //msg可以在data位置传过来
            msg = data;
            data = false;
        }
        msg = msg || '确定要变更本条数据的状态吗?';


        layer.confirm(msg, function(index) {
            layer.close(index);

            if (data) {
                var id = data.elem.name;
                var status = data.elem.checked;
                if (status == true) status = 1;
                else status = 0;
                url = url + '?id=' + id + '&status=' + status;
            }
            //向服务端发送删除指令
            $.getJSON(url, function (d) {
                layer.msg(d.msg);
                if (d.code == 0) {
                    layui.table.reload(table_id);
                }
            });
        },function(index) {
            //取消时候的处理
            if(!!data.elem) {
                //开关.存在data.elem
                data.elem.checked = data.elem.checked ? false : true;
                layui.form.render();
            } else {
                //普通按钮.直接关闭index
                layer.close(index);
            }
        });
    }

}